﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Core;
using SmartSpace.Data;

namespace SmartSpace.Workflow
{
    public class WorkflowMetadata
    {
        public Guid Id { get; set; }
        public string Caption { get; set; }

        //public virtual ActivityMetadata RootActivity { get; set; }
        public List<ActivityMetadata> Activities { get; set; }

        public EntityMetadata Entity { get; set; }
        public Guid EntityId { get; set; }

        public WorkflowMetadata()
        {
        }

        public ActivityMetadata GetRoot()
        {
            if (Activities == null)
            {
                return null;
            }

            foreach (ActivityMetadata activity in Activities)
            {
                if (activity.IsRoot)
                {
                    return activity;
                }
            }
            return null;
        }

        public ActivityMetadata Get(Guid activityId)
        {
            if (Activities == null)
            {
                return null;
            }

            foreach (ActivityMetadata activity in Activities)
            {
                if (activity.Id == activityId)
                {
                    return activity;
                }
            }
            return null;
        }

        public EventMetadata GetEvent(Guid eventId)
        {
            foreach (ActivityMetadata activity in Activities)
            {
                foreach (EventMetadata @event in activity.Events)
                {
                    if (@event.Id == eventId)
                    {
                        return @event;
                    }
                }
            }
            return null;
        }
    }
}
